package com.ssyc.student.activity;

import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.ssyc.common.base.BaseActivity;
import com.ssyc.common.base.BaseApplication;
import com.ssyc.common.glide.ImageUtil;
import com.ssyc.common.http.HttpAdress;
import com.ssyc.common.http.HttpUtils;
import com.ssyc.common.manager.SrfManager;
import com.ssyc.common.utils.GsonUtil;
import com.ssyc.common.utils.UiUtils;
import com.ssyc.student.R;
import com.ssyc.student.adapter.StPersonInfoRvAdatpter;
import com.ssyc.student.bean.PersonDetailsInfo;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import de.hdodenhof.circleimageview.CircleImageView;
import okhttp3.Call;

/**
 * 学生端---个人详情界面的界面
 * Created by Administrator on 2018/5/22 0022.
 */

@Route(path = "/stduent/StudentPersonDetailsActivity")
public class StudentPersonDetailsActivity extends BaseActivity implements View.OnClickListener {

    //加载框
    private RelativeLayout rlLoading;

    //下拉刷新控件
    private SmartRefreshLayout srf;

    //获取携带过来的账号信息,注意:金牌号的accid是有11位账号(手机号)+1位用户角色组成
    private String accid;

    //请求服务器需要的账号信息
    private String account;
    private String role;

    //关闭
    private ImageView rlClose;
    //头像
    private CircleImageView ivHead;
    //姓名
    private TextView tvName;

    //手机号
    private TextView tvPhone;

    //展示个人图片的Recycler
    private RecyclerView rv;

    //模拟的个人资料的相册信息
    private List<String> mUrls;
    private StPersonInfoRvAdatpter adatpter;

    @Override
    protected boolean isNeedShowTitle() {
        return false;
    }

    @Override
    protected String setTitleText() {
        return null;
    }

    @Override
    protected int getContentLayout() {
        return R.layout.student_activity_person_details;
    }

    @Override
    protected void init() {

        showContent();

        //初始化视图
        initView();

        //初始化下拉刷新
        initSrf();

        //设置展示个人图片资料的列表
//        initRv();

        //获取页面数据
        http();
    }

    /**
     * 获取页面数据的方法
     */
    private void http() {
        Map<String, String> map = new HashMap<>();
        map.put("type", "7");
        //这里的取值acount和role都是各个端带过来的数据
        map.put("c", account);
        map.put("role", role);
        map.put("platform", "8");

        HttpUtils.post(HttpAdress.PERSONDETAILS, map, this, new HttpUtils.CallBack() {
            @Override
            public void onError(Call call, Exception e, int id) {
                if (null != rlLoading) {
                    rlLoading.setVisibility(View.GONE);
                }
                UiUtils.Toast(BaseApplication.ERROR, false);
            }

            @Override
            public void onResponse(String response, int id) {
                if (null != rlLoading) {
                    rlLoading.setVisibility(View.GONE);
                }
                try {
                    if (!TextUtils.isEmpty(response)) {
                        PersonDetailsInfo info = GsonUtil.jsonToBean(response, PersonDetailsInfo.class);
                        if (null != info) {
                            if ("1".equals(info.getState())) {
                                //返回成功
                                setData(info);
                            } else {
                                //返回异常
                                Log.i("test", info.getState() + "");
                            }
                        }
                    }

                } catch (Exception e) {
                    e.printStackTrace();
                    Log.i("test", "数据解析异常");
                }
            }
        });


    }

    /**
     * 设置页面数据的方法
     *
     * @param info
     */
    private void setData(PersonDetailsInfo info) {
        //设置头像
        ImageUtil.displayImage(this, ivHead, info.getIcon());
        //姓名
        tvName.setText(info.getName());
        //电话号码
        String phone = info.getUser_id();
        String str = "*****";
        StringBuffer sb = new StringBuffer(phone);
        sb.replace(3, 8, str);
        tvPhone.setText(sb.toString());
    }

    /**
     * 设置展示个人图片信息的列表方法
     */
//    private void initRv() {
//        LinearLayoutManager manager = new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false);
//        rv.setLayoutManager(manager);
//        rv.setHasFixedSize(true);
//        adatpter = new StPersonInfoRvAdatpter(this, R.layout.student_rv_person_info, mUrls);
//        rv.setAdapter(adatpter);
//        adatpter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
//            @Override
//            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
//                //跳转到查看大图的界面
//                PickChooseManager.startShowBigActivity(StudentPersonDetailsActivity.this, mUrls, position);
//            }
//        });
//    }


    /**
     * 初始化下拉刷新控件
     */
    private void initSrf() {
        SrfManager.setSmartByPullListener(srf, null);
    }

    /**
     * 初始化视图
     */
    public void initView() {
        //获取通过ARouter传递过来的云信accid
        accid = getIntent().getStringExtra("accid");//length 12 索引 0 到11
        if (!TextUtils.isEmpty(accid)) {
            account = accid.substring(0, accid.length() - 1);
            role = accid.substring(accid.length() - 1);
        }
        rlLoading = findViewById(R.id.rl_loading);
        rlLoading.setVisibility(View.VISIBLE);
        rlClose = findViewById(R.id.rl_close);
        rlClose.setOnClickListener(this);
        ivHead = findViewById(R.id.iv_head);
        tvName = findViewById(R.id.tv_name);
        tvPhone = findViewById(R.id.tv_phone);
        srf = findViewById(R.id.srf);
        rv = findViewById(R.id.rv);
    }

    /**
     * 点击事件
     *
     * @param view
     */
    @Override
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.rl_close) {
            //关闭
            finish();
        }
    }
}
